//
//  MoreSkillsVC.m
//  MyProject
//
//  Created by 石小强 on 2018/3/20.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MoreSkillsVC.h"
#import "PositionSelectCell.h"
#import "PositionSelectView.h"
#import "IndustryModel.h"
#import <MJExtension.h>



@interface MoreSkillsVC ()

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *oneArray;
@property (nonatomic, strong) NSMutableArray *twoArray;
@property (nonatomic, strong) PositionSelectView *selectView;

@end

@implementation MoreSkillsVC {
    SkillModel *_selectIndustry;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择技能";
    self.view.backgroundColor = kStyleColor;
    
    //    self.dataArray = @[@"产品", @"技术", @"技术", @"技术", @"技术", @"技术", @"技术", @"技术"];
    //    self.oneArray = @[@"后端开发", @"移动开发", @"测试", @"运维/技术支持", @"前端开发", @"项目管理", @"数据库", @"硬件开发", @"H5"];
    //    self.twoArray = @[@"Java", @"C", @"C++", @"Python", @"OC", @"Swift", @"PHP", @"C#"];
    
    [self setupSubViews];
    
    // 获取行业信息
    [self loadIndustriesData:@"" array:self.dataArray];
}

- (void)setupSubViews {
    [self.oneTableView registerNib:[UINib nibWithNibName:NSStringFromClass([PositionSelectCell class]) bundle:nil] forCellReuseIdentifier:@"PositionSelectCell"];
    self.oneTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.oneTableView.backgroundColor = kStyleColor;
}

/** 获取技能信息 */
- (void)loadIndustriesData:(NSString *)skillId array:(NSMutableArray *)array {
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"skillPid":skillId} url:@"basics/getSkills" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [array removeAllObjects];
            
            NSArray *datas = [SkillModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
            [array addObjectsFromArray:datas];
            if (array == self.dataArray) {
                [self.oneTableView reloadData];
            }else if (array == self.oneArray) {
                [_selectView.cateOneTableView reloadData];
                
            }else if (array == self.twoArray) {
                [_selectView.cateTwoTableView reloadData];
            }
            
            
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
//    WS(weakSelf)
//    [self showProgressWithView:self.view animated:YES];
//    [SendRequest getIndustriesWithResult:^(NSDictionary *result, NSError *error) {
//        [weakSelf hideProgress:self.view animated:YES];
//        result = [CommonUtils decryptDicWithResult:result];
//        if([result[@"result"] integerValue] == 1)
//        {
//            SaintiLog(@"获取行业信息--返回值 = %@", result);
//            [self.dataArray removeAllObjects];
//
//            NSArray *datas = [IndustryModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
//            [self.dataArray addObjectsFromArray:datas];
//            [self.oneTableView reloadData];
//        }else{
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
//        }
//
//    }];
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.oneTableView) {
        return self.dataArray.count;
    } else if (tableView == _selectView.cateOneTableView) {
        return self.oneArray.count;
    } else {
        return self.twoArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.oneTableView) {
        PositionSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PositionSelectCell"];
        [cell setCenterNameLHidden:YES];
        SkillModel *industry = [self.dataArray objectAtIndex:indexPath.row];
        cell.nameL.text = industry.skillName;
        return cell;
    } else if (tableView == self.selectView.cateOneTableView) {
        PositionSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCateOne"];
        [cell setCenterNameLHidden:NO];
        SkillModel *job = [self.oneArray objectAtIndex:indexPath.row];
        cell.centerNameL.text = job.skillName;
        return cell;
    } else {
        PositionSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectCateTwo"];
        [cell setCenterNameLHidden:NO];
        cell.backgroundColor = kStyleColor;
        SkillModel *job = [self.twoArray objectAtIndex:indexPath.row];
        cell.centerNameL.text = job.skillName;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.oneTableView) {
        _selectIndustry = [self.dataArray objectAtIndex:indexPath.row];
        [self loadIndustriesData:[NSString stringWithFormat:@"%zd",_selectIndustry.skillId] array:self.oneArray];
        self.selectView.mj_x = 0;
        [UIView animateWithDuration:0.2 animations:^{
            self.selectView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
            self.selectView.backV.mj_x = 0;
        }];
    } else if (tableView == _selectView.cateTwoTableView) {
        self.selectBlock([self.twoArray objectAtIndex:indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    } else if (tableView == _selectView.cateOneTableView) {
        SkillModel *job = [self.oneArray objectAtIndex:indexPath.row];
          [self loadIndustriesData:[NSString stringWithFormat:@"%zd",job.skillId] array:self.twoArray];
    }
    else {
        // 刷新数据
    }
}

///** 获取职位 */
//- (void)loadJobListWithIndustryId:(NSString *)industryId {
//    [SendRequest getJobListWithIndustryId:industryId andJobPid:@"" result:^(NSDictionary *result, NSError *error) {
//        result = [CommonUtils decryptDicWithResult:result];
//        if([result[@"result"] integerValue] == 1)
//        {
//            SaintiLog(@"获取职位1--返回值 = %@", result);
//            NSArray *datas = [JobModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
//            [self.oneArray removeAllObjects];
//            [self.oneArray addObjectsFromArray:datas];
//
//            [self.selectView.cateOneTableView reloadData];
//            if (self.oneArray.count > 0) {
//                NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
//                [self.selectView.cateOneTableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionTop];
//
//                JobModel *one = [datas objectAtIndex:0];
//                [self loadJobListWithIndustryId:_selectIndustry.industryId andJobPid:one.jobId];
//            }
//        }else{
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
//        }
//    }];
//}

//- (void)loadJobListWithIndustryId:(NSString *)industryId andJobPid:(NSString *)jobPid {
//    [SendRequest getJobListWithIndustryId:industryId andJobPid:jobPid result:^(NSDictionary *result, NSError *error) {
//        result = [CommonUtils decryptDicWithResult:result];
//        if([result[@"result"] integerValue] == 1)
//        {
//            SaintiLog(@"获取职位2--返回值 = %@", result);
//            NSArray *datas = [JobModel mj_objectArrayWithKeyValuesArray:[result objectForKey:@"data"]];
//            [self.twoArray removeAllObjects];
//            [self.twoArray addObjectsFromArray:datas];
//
//            [self.selectView.cateTwoTableView reloadData];
//        }else{
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
//        }
//    }];
//}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)oneArray {
    if (!_oneArray) {
        _oneArray = [NSMutableArray array];
    }
    return _oneArray;
}

- (NSMutableArray *)twoArray {
    if (!_twoArray) {
        _twoArray = [NSMutableArray array];
    }
    return _twoArray;
}

- (PositionSelectView *)selectView {
    if (!_selectView) {
        _selectView = [[PositionSelectView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight)];
        _selectView.cateOneTableView.delegate = self;
        _selectView.cateOneTableView.dataSource = self;
        _selectView.cateTwoTableView.delegate = self;
        _selectView.cateTwoTableView.dataSource = self;
        [self.view addSubview:_selectView];
        [self.view bringSubviewToFront:_selectView];
    }
    return _selectView;
}


@end
